local sz_huadie = fk.CreateSkill {
    name = "sz_huadie",
}

Fk:loadTranslationTable {
    ["sz_huadie"] = "化蝶",
    [":sz_huadie"] = "出牌阶段限一次，你可以摸体力值等量的牌，令本回合使用牌无次数和距离限制，若如此做，回合结束时，若你本回合造成的伤害不足当前体力值，失去差值等量的体力(摸牌和失去体力均至多为5)。",

    ["@@sz_huadie-turn"] = "化蝶",
}

sz_huadie:addEffect("active", {
    anim_type = "offensive",
    prompt = function(self)
        local x = math.min(5, Self.hp)
        return "化蝶：是否摸" .. x .. "张牌，令本回合使用牌无次数和距离限制？"
    end,
    card_num = 0,
    mtarget_num = 0,
    can_use = function(self, player)
        return not player.dead and player:usedEffectTimes(sz_huadie.name, Player.HistoryPhase) == 0
    end,
    card_filter = Util.FalseFunc,
    on_use = function(self, room, effect)
        local player = effect.from
        local x = math.min(5, player.hp)
        player:drawCards(x, sz_huadie.name)
        room:setPlayerMark(player, "@@sz_huadie-turn", 1)
    end,
})

sz_huadie:addEffect(fk.EventPhaseEnd, {
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(sz_huadie.name) and player.phase == Player.Finish and
            player:getMark("@@sz_huadie-turn") > 0 and
            #player.room.logic:getActualDamageEvents(1, function(e)
                return e.data.from == player
            end, Player.HistoryTurn) <= player.hp
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local n = 0
        room.logic:getActualDamageEvents(1, function(e)
            local damage = e.data
            if damage.from == player then
                n = n + damage.damage
            end
        end, Player.HistoryTurn)
        room:loseHp(player, math.min(5, player.hp - n), sz_huadie.name)
    end,
})
sz_huadie:addEffect("targetmod", {
    bypass_times = function(self, player, skill, scope, card, to)
        return card and player:hasSkill(sz_huadie.name) and player:getMark("@@sz_huadie-turn") > 0 and
            player.phase ~= Player.NotActive
    end,
    bypass_distances = function(self, player, skill, card, to)
        return card and player:hasSkill(sz_huadie.name) and player:getMark("@@sz_huadie-turn") > 0 and
            player.phase ~= Player.NotActive
    end,
})
return sz_huadie
